<?php
namespace app\api\controller;
use app\common\model as M;

/**
 * 发送短信验证码
 */
class Login extends \app\api\ApiBase
{
    public $model = '\app\common\model\User';


    /**
     * 登录、注册
     * @return [type] [description]
     */
    public function mp ()
    {
        $code    = $this->request->post('code');

        if ( empty($code) ) return $this->returnFail('code必传!');

        try {
            $userInfo = [];
            $wechatUser = \service\WechatMicroProgramService::code2Session($code);

            if ( isset($wechatUser['openid']) ) {
                $userInfo = $this->model::wechat_login($wechatUser , $this->spread_suid , $this->spread_uid);

                if ( isset($wechatUser['session_key']) ) {
                    $userInfo['session_key'] = $wechatUser['session_key'];
                }
            }

            if ( empty($userInfo) ) return $this->returnFail('登录失败，用户不存在或自动注册失败!');
            return $this->returnSuccess($userInfo);
        } catch (\Exception $e ) {
            return $this->returnFail($e->getMessage());
        }

        return $this->returnFail('登录失败，请稍后重试!');
    }

    public function mpFake ()
    {
        try {
            $userInfo = [];
            $wechatUser = ['openid' => 'oGb0B7UVlyN03M-sbadLVMdfMHkM']; // 方老板
            //$wechatUser = ['openid' => 'oGb0B7WiBsmcxchEIQ35o_QkF5WI']; // 张明月
            $wechatUser = ['openid' => 'oGb0B7a-cn60SOyDBUGuQgzh3eps']; // 夏芳芳

            if ( isset($wechatUser['openid']) ) {
                $userInfo = $this->model::wechat_login($wechatUser , $this->spread_suid);
            }

            if ( empty($userInfo) ) return $this->returnFail('登录失败，用户不存在或自动注册失败!');
            return $this->returnSuccess($userInfo);
        } catch (\Exception $e ) {
            return $this->returnFail($e->getMessage());
        }

        return $this->returnFail('登录失败，请稍后重试!');
    }
}//Class End